package com.tally.project.system.book.controller;

import java.util.List;

import com.tally.common.enums.BookEnums;
import com.tally.framework.security.utils.SecurityUtils;
import com.tally.framework.web.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.tally.framework.aspectj.lang.annotation.Log;
import com.tally.framework.aspectj.lang.enums.BusinessType;
import com.tally.project.system.book.domain.Book;
import com.tally.project.system.book.service.IBookService;
import com.tally.framework.web.controller.BaseController;
import com.tally.framework.web.page.TableDataInfo;

/**
 * 账本Controller
 * @author soul
 * @date 2023-08-02
 */
@Api(tags = "账本")
@Controller
@RequestMapping("/system/book")
public class BookController extends BaseController
{

    @Autowired
    private IBookService bookService;

    /**
     * 查询账本列表
     */
    @ApiOperation("查询账本列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody Book book)
    {
        // 设置当前登陆用户信息ID
        book.setUserId(SecurityUtils.getUserId());
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 新增账本获取账本分类
     */
    @ApiOperation("新增-获取账本分类")
    @Log(title = "账本", businessType = BusinessType.INSERT)
    @GetMapping("/add")
    @ResponseBody
    public TableDataInfo add()
    {
        return getDataTable(BookEnums.toList());
    }

    /**
     * 新增保存账本
     */
    @ApiOperation("新增账本")
    @Log(title = "账本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Book book)
    {
        return toAjax(bookService.insertBook(book));
    }

    /**
     * 修改保存账本
     */
    @ApiOperation("修改账本")
    @Log(title = "账本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Book book)
    {
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除账本
     */
    @ApiOperation("删除账本")
    @Log(title = "账本", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bookService.deleteBookByIds(ids));
    }
}
